using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Resources;
namespace MicroMsg.UI.Page
{
	public class QQMailDetailInfoView : BasePage
	{
		private const string TAG = "QQMailDetailInfoView";
		private QQMail mMail;
		private Popup popUp;
		private string strWebContent;
		private string strInitUrl;
		private string strCurrUrl;
		private bool isLoadComplete;
		private bool hasBeenInvokLoadedComplete;
		private ScrollNewsPicListCtrl bigPicCtrl;
		private ApplicationBar mBar;
		internal Grid LayoutRoot;
		internal Grid LayoutContent;
		internal Grid webview;
		internal WebBrowser newsDetail;
		internal Canvas canvasProgressBar;
		internal PerformanceProgressBar ProgressBar;
		private bool _contentLoaded;
		public QQMailDetailInfoView()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.PhoneApplicationPage_Loaded));
			this.popUp = new Popup();
		}
		private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void web_loadedComplete(object sender, NavigationEventArgs e)
		{
			this.isLoadComplete = true;
			if (this.ProgressBar.IsIndeterminate)
			{
				this.ProgressBar.IsIndeterminate = false;
			}
		}
		private void web_Navigated(object sender, NavigationEventArgs e)
		{
			this.ProgressBar.IsIndeterminate = true;
			string originalString = e.get_Uri().get_OriginalString();
			this.strCurrUrl = originalString;
			QQMailNewMailView.updateSid(originalString);
		}
		private void newsDetail_MouseEnter(object sender, MouseEventArgs e)
		{
		}
		private void doloadedComplete()
		{
			if (this.hasBeenInvokLoadedComplete)
			{
				return;
			}
			try
			{
				if (string.IsNullOrEmpty(this.strWebContent))
				{
					this.strWebContent = this.newsDetail.SaveToString();
				}
				this.newsDetail.InvokeScript("eval", new string[]
				{
					"\r\n                window.onLinkPressed = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify(elem.getAttribute('link'));\r\n                    }\r\n                    return false;\r\n                }\r\n                window.BindLinks = function() {\r\n                    var elems = document.getElementsByTagName('img');\r\n                    for (var i = 0; i < elems.length; i++) {\r\n                        var elem = elems[i];\r\n                        var link = elem.getAttribute('src');\r\n                        elem.setAttribute('link', link);\r\n                        if(link.indexOf('.gif')>0){\r\n                            elem.parentNode.removeChild(elem);\r\n                        }else{\r\n                            elem.attachEvent('onmousedown', onLinkPressed);\r\n                        }\r\n                    }\r\n                }\r\n\r\n                window.onSharePressed = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify('blog');\r\n                    }\r\n                    return false;\r\n                }\r\n\r\n                window.onTimelinePressed = function() {\r\n                    var elem = event.srcElement;\r\n                    if ( elem != null ) {\r\n                        window.external.notify('timeline');\r\n                    }\r\n                    return false;\r\n                }\r\n                window.BindShare = function() {\r\n                    var elems = document.getElementById('share');\r\n                    if ( elems != null ) \r\n                        elems.attachEvent('onmousedown', onSharePressed);\r\n\r\n                    var elem = document.getElementById('tl');\r\n                    if ( elems != null )\r\n                        elem.attachEvent('onmousedown', onTimelinePressed);\r\n\r\n                }"
				});
				this.newsDetail.InvokeScript("BindLinks");
				this.newsDetail.InvokeScript("BindShare");
				this.hasBeenInvokLoadedComplete = true;
			}
			catch (Exception)
			{
				Log.smethod_1("QQMailDetailInfoView", "newsDetail.InvokeScript failed!");
			}
		}
		public void ShowBigPic(ScrollNewsPicsCtrlArgs scrollNewsPicsCtrlArgs_0)
		{
			if (!ScrollNewsPicsCtrlArgs.CheckIsInfoOk(scrollNewsPicsCtrlArgs_0))
			{
				return;
			}
			this.bigPicCtrl = ScrollNewsPicListCtrl.getInstance();
			this.bigPicCtrl.SetArgs(scrollNewsPicsCtrlArgs_0);
			this.bigPicCtrl.Start();
		}
		private void QQMailDetailInfoView_BackKeyPress(object sender, CancelEventArgs e)
		{
			if (this.bigPicCtrl != null && this.bigPicCtrl.mBIsHown)
			{
				this.bigPicCtrl.Hide();
				e.set_Cancel(true);
			}
		}
		private object scrollPicEnded(object obj1, object obj2)
		{
			return null;
		}
		private void SaveStringToIsoStore(string strWebContent)
		{
			IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
			if (userStoreForApplication.FileExists("web.htm"))
			{
				userStoreForApplication.DeleteFile("web.htm");
			}
			StreamResourceInfo streamResourceInfo = new StreamResourceInfo(new MemoryStream(Encoding.get_UTF8().GetBytes(strWebContent)), "html/text");
			using (BinaryReader binaryReader = new BinaryReader(streamResourceInfo.get_Stream()))
			{
				byte[] array = binaryReader.ReadBytes((int)streamResourceInfo.get_Stream().get_Length());
				using (BinaryWriter binaryWriter = new BinaryWriter(userStoreForApplication.CreateFile("web.htm")))
				{
					binaryWriter.Write(array);
					binaryWriter.Close();
				}
			}
		}
		private void btnLoad_Click(object sender, RoutedEventArgs e)
		{
			this.newsDetail.Navigate(new Uri("web.htm", 2));
		}
		protected override void OnPageIn(object[] object_0)
		{
			Log.smethod_5("QQMailDetailInfoView", "OnPageIn start");
			this.newsDetail.set_Background(R.theme.CHAT_BK_BRUSH);
			this.webview.set_Background(R.theme.CHAT_BK_BRUSH);
			if (object_0 != null && Enumerable.Count<object>(object_0) != 0)
			{
				this.mMail = (object_0[0] as QQMail);
			}
			if (this.mMail != null && !string.IsNullOrEmpty(this.mMail.strUrl))
			{
				this.strInitUrl = this.mMail.strUrl;
				base.add_BackKeyPress(new EventHandler<CancelEventArgs>(this.QQMailDetailInfoView_BackKeyPress));
				this.newsDetail.add_MouseEnter(new MouseEventHandler(this.newsDetail_MouseEnter));
				this.isLoadComplete = false;
				this.strWebContent = "";
				Uri uri = new Uri(this.mMail.strUrl);
				this.newsDetail.Navigate(uri);
				return;
			}
		}
		protected override void OnPageOut()
		{
		}
		public ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			this.mBar.set_Opacity(0.7);
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/Resource/Img/CommonUse/icon_search.png", 2));
			applicationBarIconButton.set_Text(strings.Plugin_qqmail_mail);
			applicationBarIconButton.add_Click(new EventHandler(this.backButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			ApplicationBarIconButton applicationBarIconButton2 = new ApplicationBarIconButton(new Uri("/Resource/Img/Plugin/FaceBook/icon_AppBar_Refresh.png", 2));
			applicationBarIconButton2.set_Text(strings.Set_Plugin_Setting);
			applicationBarIconButton2.add_Click(new EventHandler(this.previewButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton2);
			ApplicationBarIconButton applicationBarIconButton3 = new ApplicationBarIconButton(new Uri("/Resource/Img/Plugin/FaceBook/icon_AppBar_Refresh.png", 2));
			applicationBarIconButton3.set_Text(strings.Set_Plugin_Setting);
			applicationBarIconButton3.add_Click(new EventHandler(this.refreshButton_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton3);
			return this.mBar;
		}
		private void backButton_Click(object sender, EventArgs e)
		{
		}
		private void previewButton_Click(object sender, EventArgs e)
		{
		}
		private void refreshButton_Click(object sender, EventArgs e)
		{
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			try
			{
				if (this.strCurrUrl.Contains("mail.qq.com/cgi-bin/readmail?") && this.strCurrUrl.Contains("&s="))
				{
					this.newsDetail.InvokeScript("eval", new string[]
					{
						"history.go(-1)"
					});
					cancelEventArgs_0.set_Cancel(true);
				}
				else
				{
					cancelEventArgs_0.set_Cancel(false);
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("QQMailDetailInfoView", ex.get_Message());
				cancelEventArgs_0.set_Cancel(false);
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/QQMail/UI/QQMailDetailInfoView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.LayoutContent = (Grid)base.FindName("LayoutContent");
			this.webview = (Grid)base.FindName("webview");
			this.newsDetail = (WebBrowser)base.FindName("newsDetail");
			this.canvasProgressBar = (Canvas)base.FindName("canvasProgressBar");
			this.ProgressBar = (PerformanceProgressBar)base.FindName("ProgressBar");
		}
	}
}
